package com.landking.application;

import android.app.Application;
import android.content.Context;
import android.util.DisplayMetrics;


import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobConfig;


/**
 * Created by ywm on 2018/8/17.
 */

public class MyApplication extends Application {

    public static final String TAG = "MyApplication";
    private static Context context;
    private static MyApplication sApplication;
    public static int H, W;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        MyApplication.context = getApplicationContext();
        sApplication = this;

        // 崩溃收集
        CrashHandler.getInstance().init(this);

        // Android P（Android 9）出现Detected problems with API compatibility问题解决
        closeAndroidPDialog();

        //  获取屏幕的宽高
        getScreen(this);

        // bmob初始化操作
        bmobConfig();

        //创建默认的ImageLoader配置参数,这里是直接使用了createDefault()方法创建一个默认的
        //ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
        //创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                //.writeDebugLogs() //打印log信息
                .build();

        //调用ImageLoader的init（）方法将ImageLoaderConfiguration参数传递进去
        ImageLoader.getInstance().init(configuration);

    }

    public static Context getAppContext() {
        return MyApplication.context;
    }

    public static MyApplication getApplication() {
        return sApplication;
    }

    // Android P（Android 9）出现Detected problems with API compatibility问题解决
    private void closeAndroidPDialog() {
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bmobConfig() {

        //提供以下两种方式进行初始化操作：

        //第一：默认初始化
        // Bmob.initialize(this, "Your Application ID");
        // 注:自v3.5.2开始，数据sdk内部缝合了统计sdk，开发者无需额外集成，传渠道参数即可，不传默认没开启数据统计功能
        //Bmob.initialize(this, "Your Application ID","bmob");

        //第二：自v3.4.7版本开始,设置BmobConfig,允许设置请求超时时间、文件分片上传时每片的大小、文件的过期时间(单位为秒)，
        BmobConfig config = new BmobConfig.Builder(this)
                ////设置appkey
                .setApplicationId("c85877d0ce20a4e7ad6473311f1832fb")
                ////请求超时时间（单位为秒）：默认15s
                .setConnectTimeout(30)
                ////文件分片上传时每片的大小（单位字节），默认512*1024
                //.setUploadBlockSize(1024*1024)
                ////文件的过期时间(单位为秒)：默认1800s
                .setFileExpiration(2500)
                .build();
        Bmob.initialize(config);
    }

    //  获取屏幕的宽高
    public void getScreen(Context aty) {
        DisplayMetrics dm = aty.getResources().getDisplayMetrics();
        H = dm.heightPixels;
        W = dm.widthPixels;
    }
}
